<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/16
 * Time: 19:31
 */

namespace app\admin\model;

/**
 * 树节点模型
 * Class Node
 * @package app\admin\model
 */
class Node
{
    private $id;
    private $text;
    private $parentId;
    private $icon;
    private $type;//类型 0：菜单 1：按钮
    private $checked;//是否选中
    private $href;//链接
    private $children;//子节点列表

    public function toString1()
    {
        $result = "{"
            . "\"text\":\"" . $this->text . "\","
            . "\"icon\":\"" . $this->icon . "\","
            . "\"type\":\"" . $this->type . "\","
            . "\"href\":\"" . $this->href . "\","
            . "\"pid\":\"" . $this->parentId . "\","
            . "\"id\":\"" . $this->id . "\"";
        $len = count($this->children);
        if ($len > 0) {
            if (strpos($result,'nodes')) {
                $result = $result.",";
            }else{
                $result = $result.",\"nodes\":" . $this->toChildrenString1();
            }
        }else{
            $result=$result.",\"nodes\":[]";
        }
        return  $result . "}";
    }

    /**
     * ztree格式
     * @return string
     */
    public function toString2()
    {
        $result = "{"
            . "\"name\":\"" . $this->text . "\","
            . "\"pId\":\"" . $this->parentId . "\","
            . "\"checked\":\"" . $this->checked . "\","
            . "\"id\":\"" . $this->id . "\"";
        $len = count($this->children);
        if ($len > 0) {
            if (strpos($result,'children')) {
                $result = $result.",";
            }else{
                $result = $result.",\"children\":" . $this->toChildrenString2();
            }
        }
        return  $result . "}";
    }

    public function __construct()
    {
        $this->children = array();
        $this->checked="false";
    }

    public function toChildrenString1()
    {
        $result = "[";
        foreach ($this->children as $node) {
            $result = $result . $node->toString1() . ",";
        }
        //切割掉最后一个逗号
        $result = substr($result, 0, strlen($result) - 1);

        return $result . "]";
    }

    public function toChildrenString2()
    {
        $result = "[";
        foreach ($this->children as $node) {
            $result = $result . $node->toString2() . ",";
        }
        //切割掉最后一个逗号
        $result = substr($result, 0, strlen($result) - 1);

        return $result . "]";
    }

    /**
     * @return mixed
     */
    public function getHref()
    {
        return $this->href;
    }

    /**
     * @param mixed $href
     */
    public function setHref($href)
    {
        $this->href = $href;
    }


    /**
     * @return mixed
     */
    public function getChecked()
    {
        return $this->checked;
    }

    /**
     * @param mixed $checked
     */
    public function setChecked($checked)
    {
        $this->checked = $checked;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getText()
    {
        return $this->text;
    }

    /**
     * @param mixed $text
     */
    public function setText($text)
    {
        $this->text = $text;
    }

    /**
     * @return mixed
     */
    public function getParentId()
    {
        return $this->parentId;
    }

    /**
     * @param mixed $parentId
     */
    public function setParentId($parentId)
    {
        $this->parentId = $parentId;
    }

    /**
     * @return mixed
     */
    public function getChildren()
    {
        return $this->children;
    }

    /**
     * @param mixed $children
     */
    public function setChildren($children)
    {
        $this->children = $children;
    }

    /**
     * @return mixed
     */
    public function getIcon()
    {
        return $this->icon;
    }

    /**
     * @param mixed $icon
     */
    public function setIcon($icon)
    {
        $this->icon = $icon;
    }

    /**
     * @return mixed
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @param mixed $type
     */
    public function setType($type)
    {
        $this->type = $type;
    }

}